<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="所属题库:">
              <el-select v-model="where.topic_id" clearable filterable placeholder="请选择所属题库">
                <el-option v-for="item in topicOption" :key="item.id" :label="item.title" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="处方名称:">
              <el-input clearable v-model="where.title" placeholder="请输入处方名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="12">
            <el-form-item label="处方状态:">
              <el-radio-group v-model="where.status" size="medium">
                <el-radio-button label="">全部</el-radio-button>
                <el-radio-button :label="1">启用</el-radio-button>
                <el-radio-button :label="2">停用</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :md="12">
            <el-form-item label="处方类型:">
              <el-radio-group v-model="where.type" size="medium">
                <el-radio-button label="">全部</el-radio-button>
                <el-radio-button :label="1">考试处方</el-radio-button>
                <el-radio-button :label="2">练习处方</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)" >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button @click="removeBatch" type="danger" icon="el-icon-delete" class="ele-btn-icon" size="small"
            v-if="permission.includes('sys:prescription:dall')">批量删除
          </el-button>
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:prescription:add')">添加处方
          </el-button>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{row}">
          <el-switch v-model="row.status" @change="editStatus(row)" :active-value="1" :inactive-value="2" />
        </template>
        <template slot="problem_img" slot-scope="{ row }">
          <el-image :src="row.problem_img" :preview-src-list="[row.problem_img]" style="height: 40px;">
          </el-image>
        </template>
        <template slot="type" slot-scope="{row}">
          <el-tag :type="row.type | typeTagFilter" size="mini" effect="dark">{{ row.type | typeFilter }}</el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:prescription:edit')">修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此题库吗？" @confirm="remove(row)">
            <el-link type="danger" slot="reference" :underline="false" icon="el-icon-delete"
              v-if="permission.includes('sys:prescription:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <prescription-edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import PrescriptionEdit from './prescription-edit';
export default {
  name: 'Prescription',
  components: { PrescriptionEdit },
  computed: {
    ...mapGetters(["permission"]),
  },
  filters: {
    // 状态过滤   <!-- 用户状态 1 启用 2 冻结 3 注销 -->
    statusFilter(status) {
      const statusMap = {
        1: '启用',
        2: '禁用'
      }
      return statusMap[status]
    },
    // 考试类型 1 考试题库 2 练习题库
    typeFilter(status) {
      const statusMap = {
        1: '考试题库',
        2: '练习题库',
      }
      return statusMap[status]
    },
    // 状态类型过滤
    typeTagFilter(type) {
      const statusMap = {
        1: 'success',
        2: 'danger',
        3: 'warning '
      }
      return statusMap[type]
    }
  },
  data() {
    return {
      // 表格数据接口
      url: '/prescription/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: '编号',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'topic_name',
          label: '所属题库',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'title',
          label: '处方名称',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'prescript_num',
          label: '同库处方',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 80,
        },
        {
          prop: "problem_img",
          label: "处方图",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 60,
          slot: "problem_img",
        },
        {
          prop: 'all_score',
          label: '处方分值',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: 'num',
          label: '处方题数',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'type',
          label: '处方类型',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'type',
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        },
      ],
      // 表格搜索条件
      where: {
        type: "",
        status: "",
      },
      // 表格选中数据
      selection: [],
      topicOption: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
      maxHeight: window.innerHeight - 125 // table高度
    };
  },
  mounted() {
    window.onresize = () => {
      this.maxHeight = window.innerHeight - 125
    }
  },
  // 页面创建时
  created() {
    this.topicList();
  },
  methods: {
    /* 获取题库列表 */
    topicList() {
      this.$http
        .get("/topic/getTopicSelectList")
        .then((res) => {
          if (res.data.code === 0) {
            this.topicOption = res.data.data;
          }
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        type: "",
        status: ""
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http.post('/prescription/delete', { id: row.id }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据')
        return;
      }
      this.$confirm('确定要删除选中的题库吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http.post('/prescription/delete', { id: this.selection.map(d => d.id) }).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: 'success', message: res.data.msg });
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        "id": row.id,
        "status": row.status
      })
      this.$http.post('/prescription/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message({ type: 'success', message: res.data.msg });
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    }
  }
}
</script>
<style scoped>
</style>
